package com.kalix.btzjjj.txt.biz;

import com.kalix.btzjjj.txt.api.biz.INetworkSummaryBeanService;
import com.kalix.btzjjj.txt.api.dao.INetworkSummaryBeanDao;
import com.kalix.btzjjj.txt.api.model.InfUtil;
import com.kalix.btzjjj.txt.entities.NetworkSummaryBean;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;

import java.text.DecimalFormat;
/**
 *@ Author  ZhaoHang
 * @Date  2019/8/28
 **/
public class NetworkSummaryBeanServiceImpl extends GenericBizServiceImpl<INetworkSummaryBeanDao, NetworkSummaryBean> implements INetworkSummaryBeanService {
//    private String txtIp=null;
    String txtIp = (String) ConfigUtil.getConfigProp("FREEBOARD_TXT_IP","config.btzjjj.portconf");
    @Override
    public String getValue() {
//        JSONArray jArray= ReadFileUtil.ReadFileUtil();
//        txtIp = (String) ConfigUtil.getConfigProp("FREEBOARD_TXT_IP","config.btzjjj.txt");
        String last="";
        String str = InfUtil.interfaceUtil(txtIp,"");
        String key = str.substring(0, 304);
        String all = str.substring(304, str.length());
        String[] name = key.split(",");
        String[] value = all.split(",");
        String maxFlowLine=null;
        String nearestAlarmNetworkDevice=null;
        String totalInFlows=null;
        String networkDeviceAlarmPercent=null;
        String networkDeviceCount=null;
        String shortestUptimeNetworkDevice=null;
        String totalOutFlows=null;
        for(int i=0;i<name.length;i++){
            if("maxFlowLine".equals(name[i])){
//                    if(("-").equals(value[i])){
//                        maxFlowLine="-";
//                    }else{
                    maxFlowLine=value[i];
            }
            if("nearestAlarmNetworkDevice".equals(name[i])){
//                    if(("").equals(value[i])||value[i]==null){
//                        nearestAlarmNetworkDevice="-";
//                    }else{
                    nearestAlarmNetworkDevice=value[i];
            }
            if("totalInFlows".equals(name[i])){
                totalInFlows=value[i];
            }
            if("networkDeviceAlarmPercent".equals(name[i])){
//                    if(("0").equals(value[i])){
//                        networkDeviceAlarmPercent="-";
//                    }else{
                    networkDeviceAlarmPercent=value[i];
            }
            if("networkDeviceCount".equals(name[i])){
                networkDeviceCount=value[i];
            }
            if("shortestUptimeNetworkDevice".equals(name[i])){
                if(("").equals(value[i])||value[i]==null){
                    shortestUptimeNetworkDevice="-";
                }else{
                    shortestUptimeNetworkDevice=value[i];
                }
            }
            if("totalOutFlows".equals(name[i])){
                totalOutFlows=value[i];
            }
        }
        DecimalFormat df = new DecimalFormat("#.0");
        last="{" +
             "\"networkDeviceCount\": \""+networkDeviceCount+"\"," +
             "\"totalOutFlows\": \""+totalOutFlows+"\"," +
             "\"totalInFlows\": \""+totalInFlows+"\"," +
             "\"maxFlowLine\": \""+maxFlowLine+"\"," +
             "\"shortestUptimeNetworkDevice\": \""+shortestUptimeNetworkDevice+"\"," +
             "\"nearestAlarmNetworkDevice\": \""+nearestAlarmNetworkDevice+"\"," +
             "\"networkDeviceAlarmPercent\": \""+networkDeviceAlarmPercent.replace("%","")+"\"," +
             "\"data\": [" +
             "{" +
             "\"value\": \""+String.valueOf(df.format(Double.parseDouble(networkDeviceAlarmPercent.replace("%",""))))+"\"," +
             "\"name\": \"报警设备\"" +
             "}," +
             "{" +
             "\"value\": \""+String.valueOf((100.0-Double.parseDouble(networkDeviceAlarmPercent.replace("%",""))))+"\"," +
             "\"name\": \"正常设备\"" +
             "}" +
             "]" +
             "}";
        return last;
    }
}
